package com.workpaper.app.login.view;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.workpaper.app.exceptions.BusinessException;
import com.workpaper.app.login.service.LoginService;

public class LoginDialog extends Dialog{
	private Text userNameText;
	private Text passwordText;
	private Label tipLabel;
	public LoginDialog(Shell parentShell) {
		super(parentShell);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(2, false));
		
		Label userNameLabel = new Label(container, SWT.NONE);
		userNameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		userNameLabel.setText("账号");
		
		userNameText = new Text(container, SWT.BORDER);
		userNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label passwordLabel = new Label(container, SWT.NONE);
		passwordLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		passwordLabel.setText("密码");
		
		passwordText = new Text(container, SWT.BORDER);
		passwordText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		tipLabel = new Label(container, SWT.NONE);
		tipLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
		tipLabel.setText("提示信息.........................");
		
		
		return container;
	}

	

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, "登录", true);
		createButton(parent, IDialogConstants.CANCEL_ID, "取消", false);
	}


	@Override
	protected void okPressed() {
		// TODO Auto-generated method stub
		String userName = this.userNameText.getText();
		String password = this.passwordText.getText();
		try {
			LoginService.getInstence().doLogin(userName, password);
			super.okPressed();
		} catch (BusinessException e) {
			tipLabel.setText(e.getMessage());
		}
	}

	
	
	
}
